<?php


namespace app\store\controller;
use app\store\model\Activity;


class Activitys extends Controller
{
    public function index() {
        $typeData = array('1'=>"转盘");
        $list = $this->getlist($this->request->param());
        $this->assign("dlist",$list);
        $this->assign("typeData",$typeData);
        return $this->fetch();
    }


    /**
     * 添加 function
     *
     * @return void
     */
    public function add(){
        if($this->postData('data') !=null){
            $param = $this->postData('data');
            if(empty($param['title'])){
                return $this->renderError("活动名称不能为空！");
            }
            if(empty($param['start_time'])){
                return $this->renderError("活动时间不能为空");
            }
            $res = $this->addDataFun($param);
            
            if($res){
                return $this->renderSuccess('添加成功', url('activitys/index'));
            }else{
                return $this->renderError("活动时间不能为空");
            }
        }
        return $this->fetch();
    } 

    /**
     * 编辑
     * @param $goods_id
     * @return array|bool|mixed
     */
    public function edit($id)
    {   
        if (!$this->postData('data')) {
            $cdata = $this->getEditData($id);
            $this->assign('model',$cdata);
            $this->assign('id',$id);
            return $this->fetch();
        }
        // 更新记录
        if ($this->editFun($id,$this->postData('data'))) {

            return $this->renderSuccess('更新成功', url('activitys/index'),null);
        }
        return $this->renderError('更新失败');
    }


    /**
     * 删除商品
     * @param $goods_id
     * @return array
     */
    public function delete($id)
    {
        // 商品详情
        $wdata = array('id'=>$id);
        if (!Activity::delData($wdata)) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }



    /**
     * Undocumented function
     *
     * @param [type] $param
     * @return void
     */
    public function getlist($param){
        return Activity::getAlist();
    }

    /**
     * 添加数据
     *
     * @param [type] $param
     * @return void
     */
    public function addDataFun($param){
        $param['types'] = 1;
        $param['is_use'] = 1;
        $param['create_time'] = date("Y-m-d H:i:s");
        $param['update_time'] = date("Y-m-d H:i:s");
        return Activity::addData($param);
    }

    /**
     * 获取修改数据 function
     *
     * @param [type] $id
     * @return void
     */
    public function getEditData($id){
        $wdata = array('id'=>$id);
        return Activity::getOneData($wdata);
    }

    /**
     * 修改数据 function
     *
     * @param [type] $id
     * @param [type] $param
     * @return void
     */
    public function editFun($id,$param){
        $wdata = array('id'=>$id);
        $udata['title'] = $param['title'];
        $udata['start_time'] = $param['start_time'];
        $udata['types'] = 1;
        $udata['is_use'] = $param['is_use'];
        $udata['update_time'] = date("Y-m-d H:i:s");
        return Activity::editData($wdata,$udata);
    }
}